/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.cicp.util.pool;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.pool.ObjectPool;
import org.apache.commons.pool.impl.GenericObjectPool;
import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.util.telnet.JtaSSH;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author wangyazhou
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-10-24 下午1:37:49          wangyazhou        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class SSHConnPool {

    private static final Logger logger = Logger.getLogger(SSHConnPool.class);

    private int outTime = 60 * 1000;

    private static SSHConnPool instance = null;

    private Map<String, ObjectPool> map = new HashMap<String, ObjectPool>();

    private Object _lock = new Object();

    private SSHConnPool() {

    }

    public static synchronized SSHConnPool getInstance() {
        if (instance == null) {
            instance = new SSHConnPool();
        }
        return instance;
    }

    /**
     * 获取一个连接
     * @param host
     * @param port
     * @param user
     * @param pwd
     * @param maxSize
     * @param prompt
     * @return
     * @throws Exception
     */
    public JtaSSH getTelnet(String host, int port, String user, String pwd, int maxSize, String prompt) throws Exception {
        ObjectPool pool = map.get(host);
        if (pool == null) {
            synchronized (_lock) {
                if (pool == null) {
                    SSHPoolableObject factory = new SSHPoolableObject(host, port, user, pwd, prompt);
                    GenericObjectPool.Config config = new GenericObjectPool.Config();
                    config.lifo = false;
                    config.maxActive = maxSize;
                    config.maxIdle = maxSize;
                    config.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_BLOCK;
                    config.minIdle = 1;
                    config.testOnReturn = true;
                    config.maxWait = outTime;
                    pool = new GenericObjectPool(factory, config);
                    map.put(host, pool);
                }
            }
        }
        return (JtaSSH) pool.borrowObject();
    }

    /**
     * 
     * @param ip
     */
    public void updateUser(String ip) {
        ObjectPool pool = map.get(ip);
        try {
            logger.info("[TIP]\t重建连接池...");
            pool.close();
            map.put(ip, null);
        } catch (Exception e) {
            logger.error(e);
        }
    }

    /**
     * 
     * @param ip
     * @param ssh
     */
    public void free(String ip, JtaSSH ssh) {
        ObjectPool pool = map.get(ip);
        if (ssh == null) {
            return;
        }
        try {
            pool.returnObject(ssh);
        } catch (Exception e) {
            logger.error(e);
        }
    }

}
